package dream.web;

import dream.pojo.AppVar;
import dream.service.ArticleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;


@WebServlet("/loginout")
public class LoginOutServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession(false);
        if(session!=null && session.getAttribute(AppVar.SESSION_USERINFO_KEY)!=null){
            //说明当前是登录状态，清空session
            session.setAttribute(AppVar.SESSION_USERINFO_KEY,null);

            //清除Cookie
            Cookie newCookie1 = new Cookie("id","");
            //立即删除型
            newCookie1.setMaxAge(0);
            //重新写入，将覆盖之前的
            resp.addCookie(newCookie1);


            //清除Cookie
            Cookie newCookie2 = new Cookie("ids","");
            //立即删除型
            newCookie2.setMaxAge(0);
            //重新写入，将覆盖之前的
            resp.addCookie(newCookie2);

            resp.getWriter().println("1");

        }else{
            return;
        }
    }
}
